//
//  BBSListBaseViewController.m
//  TianyaQing
//
//  Created by Liccon Chang on 12-3-16.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "BBSListBaseViewController.h"
#import "BBSListTableViewCell.h"
#import "DetailViewController.h"
#import "UIColor+HTML.h"
#import "PageData.h"

@implementation BBSListBaseViewController

@synthesize categoryId=categoryId_;
@synthesize categoryName=categoryName_;

- (void)viewDidLoad 
{
    [super viewDidLoad];
    totalCount_=500;
    mainTable_.backgroundColor = [UIColor colorWithHTMLName:@"#F5F5F5"];
    [self requestListData:TRUE];
}

#pragma mark -
#pragma mark ListNetworkViewController
- (BOOL)requestListData:(BOOL)refresh
{
    if (feeds_==nil)
    {
        [self showWaitingView];
    }
    return TRUE;
}

- (BOOL)canShowGetMore
{
    return totalCount_>[feeds_ count];
}

- (void)showGetMoreWaittingView
{
    if ([self canShowGetMore])  
    {
        [super showGetMoreWaittingView];
    }
}

#pragma mark -
#pragma mark ASIHTTPRequest
- (void)requestFinished:(ASIHTTPRequest *)request 
{
	[DialogUtil hideWaitingView];
    [self hideWaitingView];
	NSLog(@"%@ \n requestFinished",[request responseString]);	
	BaseResponseData* data=[BaseResponseData responseDataWithRequest:request];
	if([data success])
	{
        PageData* pagedData=[PageData pageDataWithNoteInfoListDataDic:data.dataDic];
		totalCount_=pagedData.total>0?pagedData.total:totalCount_;
        pageIndex_++;
        if (listFeeds_==nil)
        {
            listFeeds_=[[NSMutableArray alloc] initWithCapacity:[pagedData.array count]];
            feeds_=listFeeds_;
        }
        if (isRefresh_)//如果是刷新，则清空原来的数据
        {
            [listFeeds_ removeAllObjects];
        }
        [listFeeds_ addObjectsFromArray:pagedData.array];
        [mainTable_ reloadData];
        [self hideGetMoreWaittingView];
	}
	else
	{
        //[self requestFailed:request];
		//[DialogUtil postAlertWithMessage:[data errorMessage]];
        [DialogUtil postAlertWithMessage:@"获取数据失败"];
	}    
    listRequest_=nil;
}

- (void)requestFailed:(ASIHTTPRequest *)request 
{
	[DialogUtil hideWaitingView];
    [self hideWaitingView];
	NSLog(@"%@ \n requestFailed",[request responseString]);
	[DialogUtil postAlertWithMessage:@"获取数据失败" afterDelay:0.5];
    [self hideGetMoreWaittingView];
    listRequest_=nil;
}

#pragma mark -
#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath 
{
	return [BBSListTableViewCell heightForTableViewCell];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{    
	static NSString *CellIdentifier = @"BBSListTableViewCell";    
    BBSListTableViewCell *cell = (BBSListTableViewCell*)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil)
    {
        cell = [[[BBSListTableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                            reuseIdentifier:CellIdentifier] autorelease];
        cell.selectionStyle = UITableViewCellSelectionStyleGray;
    }
    NoteInfo* item = (NoteInfo*)[listFeeds_ objectAtIndex:[indexPath row]];
    [cell initDataWithNoteInfo:item];
//	cell.textLabel.text=item.title;
//    cell.authorNameLabel.text=item.author;
//    if ([item.createTime length]>16)
//    {
//        cell.createTimeLabel.text=[item.createTime substringToIndex:16];
//        //NSRange range=NSMakeRange(5,11);
//        //cell.createTimeLabel.text=[item.createTime substringWithRange:range];
//    }
//    else
//    {
//        cell.createTimeLabel.text=item.createTime;
//    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{
	[tableView deselectRowAtIndexPath:indexPath animated:FALSE];

	NoteInfo* noteInfo=[feeds_ objectAtIndex:[indexPath row]];
	
	DetailViewController* detailViewController=[[DetailViewController alloc] initWithNoteInfo:noteInfo];
	[self.navigationController pushViewController:detailViewController animated:YES];
	[detailViewController release];
    
    if (![noteInfo isExistInDatabase])
    {
        [noteInfo addToHistroyRecord];
    }
}

#pragma mark -
#pragma mark AdSageRecommendDelegate
- (UIViewController *)viewControllerForPresentingModalView
{
    return self.tabBarController;
}


#pragma mark -
#pragma mark UIViewController
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)dealloc 
{
    [listFeeds_ release];
    [categoryId_ release];
    if (listRequest_)
    {
        [listRequest_ clearDelegatesAndCancel];
    }
	[super dealloc];
}

@end
